package util
{
	import event.GlobalEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	import flash3.framework.IUpdate;
	import flash3.framework.UpdateManager;

	/**
	 *此类为成绩累加专用
	 * @author Administrator
	 * 
	 */	
	public class EffectCharAdd extends EventDispatcher implements IUpdate
	{
		private var _hasinit:Boolean;
		
		private var _field:TextField;		//文本
		private var _delay:Number;			//延迟时间
		private var _timer:Timer;			//计时器
		private var _rate:Number;			//每次增加多少
		private var _max:Number;			//目标
		private var _min:Number;			//目标
		private var _cur:Number;			//当前
		private var _type:Boolean;			//true：升序 false：降序
		private var _finish:Boolean;
		private var _playSound:Boolean;
		private var _stop:Boolean = false;
		private var _curTime:Number;
		
		public static const TIMER_COUNT:String = "timer_count";
		
		public function EffectCharAdd(field:TextField, delay:Number, rate:Number, max:Number, min:Number, type:Boolean)
		{
			_field = field;
			_delay = delay;
			_rate  = rate;
			_max   = max;
			_min   = min;
			_type  = type;
			_finish = false;
			_stop = true;
			if(_type)
			{
				_cur = min;
			}
			else
			{
				_cur = max;
			}
			
			_hasinit = true;
			UpdateManager.i.add(this);
		}
		
		public function setRangle(max:Number, min:Number):void
		{
			_max = max;
			_min = min;
		}
		
		public function update(lastTime:int):void
		{
			if(!_hasinit || _finish || _stop)
				return;
			if(_type)
			{
				if((_curTime += lastTime) > _delay)
				{
					_cur += _rate;
					_curTime = 0;
					if(_cur <= _max)
					{
						var evt:GlobalEvent = new GlobalEvent(TIMER_COUNT);
						evt.data = _cur;
						this.dispatchEvent(evt);
					}
				}
				
				if(_cur > _max)
				{
					this.dispatchEvent(new Event(Event.COMPLETE));
					_finish = true;
					_cur = _max;
					_timer.stop();
				}
			}
			else
			{
				if((_curTime += lastTime) > _delay)
				{
					_cur -= _rate;
					_curTime = 0;
					if(_cur >= _min)
					{
						evt = new GlobalEvent(TIMER_COUNT);
						evt.data = _cur;
						this.dispatchEvent(evt);
					}
				}
				
				if(_cur < _min)
				{
					this.dispatchEvent(new Event(Event.COMPLETE));
					_finish = true;
					_cur = _min;
				}
			}
			if(_field)
				_field.text = _cur.toString();
		}
		public function stop():void
		{
			_stop = true;
		}
		
		public function start():void
		{
			reset();
		}
		
		public function get getTimer():Timer
		{
			return _timer;
			
		}
		
		public function get finish():Boolean
		{
			return _finish;
		}
		
		public function reset():void
		{
			_finish = false;
			_hasinit = true;
			_stop = false;
			_curTime = 0;
			if(_type)
			{
				_cur = _min;
			}
			else
			{
				_cur = _max;
			}
			if(_field)
				_field.text = _cur.toString();
		}
	}
}